package com.example.tests;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import java.util.Set;

import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class ContactCreationTests extends TestBase {

	Random rnd = new Random();
	
	@DataProvider(name = "randomContacts")
	public Iterator<Object[]> generateRandomGroups() {
		List<Object[]> list = new ArrayList<Object[]>();
		for (int i = 0; i < 3; i++){
			ContactObject contact = new ContactObject()
				.setFirstname("FN"+rnd.nextInt())
				.setLastname("LN"+rnd.nextInt())
				.setAddress("Ad"+rnd.nextInt())
				.setHome("Hm"+rnd.nextInt())
				.setMobile("Mb"+rnd.nextInt())
				.setWork("Wr"+rnd.nextInt())
				.setEmail("Em"+rnd.nextInt())
				.setEmail2("Em2"+rnd.nextInt())
				.setBday("1")
				.setBmonth("January")
				.setByear("2001")
				.setGroup("Gr"+rnd.nextInt())
				.setAddress2("Somewhere else")
				.setPhone2("P2"+rnd.nextInt());
			list.add(new Object[]{contact});
		}
	return list.iterator();
	} 
		
	@Test(dataProvider = "randomContacts")
	public void testValidContactCanBeCreated(ContactObject validContact) throws Exception {
		app.getGroupHelper().ifGroupNotExist(validContact.group);
		Set<ContactObject> oldList = app.getContactHelper().getContacts();
		//action
		app.getContactHelper().createContact(validContact);
		Set<ContactObject> newList = app.getContactHelper().getContacts();
		verifyContactAdded(oldList, validContact, newList);
	}

    @Test
    public void testContactWithEmptyDataCanBeCreated() throws Exception {
    	ContactObject emptyDataContact = new ContactObject()
			.setFirstname("")
			.setLastname("")
			.setAddress("")
			.setHome("")
			.setMobile("")
			.setWork("")
			.setEmail("")
			.setEmail2("")
			.setBday("-")
			.setBmonth("-")
			.setByear("")
			.setGroup("[none]")
			.setAddress2("")
			.setPhone2("");
		Set<ContactObject> oldList = app.getContactHelper().getContacts();    	
	    app.getContactHelper().createContact(emptyDataContact);
		Set<ContactObject> newList = app.getContactHelper().getContacts();
		verifyContactAdded(oldList, emptyDataContact, newList);
    }	
	
}